package com.polylink.sys.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.mapper.SqlMapper;
import com.polylink.base.Page;
import com.polylink.sys.bean.User;
import com.polylink.sys.dao.sqlProvider.UserSqlProvider;

/**
 *@author xiliang.xiao
 *@date 2015年3月23日 上午9:03:57
 *
 **/
public interface IUserDao extends SqlMapper{

	@Insert("Insert into cti_web_user (username,password,mail,phone) value(#{name},#{psw},#{mail},#{phone})")
	@SelectKey(keyProperty="id", before = false, resultType = Integer.class, statement = "SELECT @@IDENTITY")
	public int add(User user);

	@SelectProvider(type=UserSqlProvider.class,method="list")
	public abstract List<User> list(Page p) ;

	@SelectProvider(type=UserSqlProvider.class,method="count")
	public int count(Page p);

	@Update("update cti_web_user set username=#{name},mail=#{mail},phone=#{phone} where id=#{id}")
	public int update(User user);
	
	@Update("update cti_web_user set password=#{psw} where id=#{id}")
	public int updatePsw(User user);

	@Delete("delete from cti_web_user where id in (${id})")
	public int delete(@Param("id")String id);

	@Select("select id,username name,password psw,mail,phone from cti_web_user where username=#{username}")
	public User findUserByUsername(String username);

	/**
	 * 获得用户拥有的权限资源
	 * @param username
	 * @return
	 */
	@Select("SELECT u.id,u.username,f.`code`,f.id,r.rolename ,m.remark,mp.remark premark from cti_web_user u "+
		"INNER JOIN cti_web_user_role ur on u.id = ur.user_id "+
		"INNER JOIN cti_web_role r on ur.role_id = r.id "+
		"INNER JOIN cti_web_role_function rf on r.id = rf.role_id "+
		"INNER JOIN cti_web_function f on rf.function_id = f.id "+ 
		"LEFT JOIN cti_web_model m on f.mode_id=m.id "+
		"LEFT JOIN cti_web_model mp on m.parent_id=mp.id "+
		"where u.username=#{username} ")
	public List<Map<String, Object>> findUserFunctionByUsername(String username);

	/**
	 * 获得用户角色权限
	 * @param id
	 * @return
	 */
	@Select("select r.id,r.rolename,ur.user_id from cti_web_role r left join cti_web_user_role ur on r.id=ur.role_id and ur.user_id=#{id}")
	public List<Map<String, Object>> getRoleOutUser(String id);

	/**
	 * 获得用户关联的对象ID
	 * @param userId
	 * @return
	 */
	@Select("select ur.user_id,ur.role_id from cti_web_user_role ur where ur.user_id=#{userId}")
	public List<Map<String, Object>> getUserRole(String userId);

	@Insert("insert into cti_web_user_role(role_id,user_id) value(#{roleId},#{id})")
	public void addUserRole(@Param("roleId")String roleId, @Param("id")String id);

	@Delete("delete from cti_web_user_role where role_id=#{roleId} and user_id=#{id}")
	public void delUserRole(@Param("roleId")String roleId, @Param("id")String id);

	@Update("update cti_web_user set password = #{psw} where id=#{id}")
	public int resetPsw(@Param("id")String id,@Param("psw") String psw);

}
